package com.yunhai.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunhai.common.dto.GlobalException;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.common.utils.ReflectionCopy;
import com.yunhai.device.mapper.OrrMaterialSummaryMapper;
import com.yunhai.device.service.OrrMaterialSummaryService;
import com.yunhai.model.entity.orr.OrrMaterialSummaryEntity;
import com.yunhai.model.vo.orr.OrrMaterialSummaryQuery;
import com.yunhai.model.vo.orr.OrrMaterialSummaryVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OrrMaterialSummaryServiceImpl implements OrrMaterialSummaryService {

    @Resource
    private OrrMaterialSummaryMapper orrMaterialSummaryMapper;

    /**
     * 获取 OrrMaterialSummary 分页列表
     */
    @Transactional
    public IPage getOrrMaterialSummaryPagination(PaginationVo paginationVo, OrrMaterialSummaryQuery orrMaterialSummaryQuery ) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<OrrMaterialSummaryEntity> lqs = new LambdaQueryWrapper();
        orrMaterialSummaryMapper.selectPage(page, lqs);

        List<OrrMaterialSummaryEntity> record = page.getRecords();
        if( !ObjectUtil.isNotEmptyList(record)){
            return page;
        }
        return page;
    }


    /**
     * 获取 OrrMaterialSummary 所有数据
     *
     */
    @Transactional
    public List<OrrMaterialSummaryEntity> getOrrMaterialSummaryAllList( OrrMaterialSummaryQuery orrMaterialSummaryQuery ) throws Exception {

        LambdaQueryWrapper<OrrMaterialSummaryEntity> lqs = new LambdaQueryWrapper();
        List<OrrMaterialSummaryEntity> list = orrMaterialSummaryMapper.selectList(lqs);
        return list;
    }


    /**
     * 获取 OrrMaterialSummary 详情
     */
    @Transactional
    public OrrMaterialSummaryEntity getOrrMaterialSummaryDetail( Long id) throws Exception {

        OrrMaterialSummaryEntity orrMaterialSummaryEntity = orrMaterialSummaryMapper.selectById(id);
        return orrMaterialSummaryEntity;
    }


    /**
     * 新增 OrrMaterialSummary
     * */
    @Transactional
    public void addOrrMaterialSummary(OrrMaterialSummaryVo orrMaterialSummaryVo) throws Exception {

        OrrMaterialSummaryEntity orrMaterialSummaryEntity = new OrrMaterialSummaryEntity();
        ReflectionCopy.copyProperties( orrMaterialSummaryEntity, orrMaterialSummaryVo );
        orrMaterialSummaryMapper.insert(orrMaterialSummaryEntity);
        return;
    }


    /**
     * 更新 OrrMaterialSummary
     * */
    @Transactional
    public void updateOrrMaterialSummary( OrrMaterialSummaryVo orrMaterialSummaryVo) throws Exception {

        if( orrMaterialSummaryVo.getId() == null ){
            throw GlobalException.gen(10021, "id不存在");
        }
        OrrMaterialSummaryEntity orrMaterialSummaryEntity = new OrrMaterialSummaryEntity();
        ReflectionCopy.copyProperties( orrMaterialSummaryEntity, orrMaterialSummaryVo );
        orrMaterialSummaryMapper.updateById(orrMaterialSummaryEntity);
        return;
    }

    /**
     * 删除 OrrMaterialSummary
     * */
    @Transactional
    public void batchRemoveOrrMaterialSummary( List<Long> ids ) throws Exception {

        if( !ObjectUtil.isNotEmptyList(ids) ){
            throw GlobalException.gen(10011, "要删除的id不存在");
        }
        orrMaterialSummaryMapper.deleteBatchIds(ids);
    }

}